# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=xmlsec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.37
pkgrel=4
pkgdesc="XML Security Library is a C library based on LibXML2 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.aleksey.com/xmlsec/"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-libltdl")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://www.aleksey.com/xmlsec/download/${_realname}1-${pkgver}.tar.gz
        01-include-openssl-last.patch
        02-make-new-wincrypt-functions-available.patch)
sha256sums=('5f8dfbcb6d1e56bddd0b5ec2e00a3d0ca5342a9f57c24dffde5c796b2be2871c'
            '015473ce9ad0110ebf4a43be3d3046b5616345799879512d5da22a71b4cecc35'
            'c3297ca54a8baa50a880da35d546868faf7579d0b60b143385892034927fb9f3')

prepare() {
  cd "${srcdir}/${_realname}1-${pkgver}"
  patch -p1 -i ${srcdir}/01-include-openssl-last.patch
  patch -p1 -i ${srcdir}/02-make-new-wincrypt-functions-available.patch
  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DLIBXML_STATIC -DLIBXSLT_STATIC -DXMLSEC_STATIC" \
  ../${_realname}1-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --enable-static-linking \
    --enable-mscrypto \
    --enable-mscng

  make

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../${_realname}1-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --enable-mscrypto \
    --enable-mscng

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make check || warning "Tests failed"

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make check || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR=${pkgdir} install

  # copy manually copyright files
  install -Dm644 "${srcdir}/${_realname}1-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}1-${pkgver}/Copyright" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Copyright"

  # Remove hard coded library path from pkg-config files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-gcrypt.pc
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ./xmlsec1-gcrypt.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-gnutls.pc
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ./xmlsec1-gnutls.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1-openssl.pc
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ./xmlsec1-openssl.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./xmlsec1.pc
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ../xmlsec1Conf.sh
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i ../xmlsec1Conf.sh
  popd > /dev/null
  pushd "${pkgdir}/${MINGW_PREFIX}/bin" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i xmlsec1-config
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i xmlsec1-config
  popd
}
